package com.v1.video.activity;

import java.util.HashMap;

import android.app.Activity;
import android.os.Handler.Callback;
import android.os.Message;
import android.widget.Toast;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

import com.iss.view.common.ToastAlone;
import com.mob.tools.utils.UIHandler;
import com.umeng.analytics.MobclickAgent;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.util.Logger;

public class SharePlatformActionListener implements PlatformActionListener,
		Callback {
	private final String TAG = "SharePlatformActionListener";
	private Activity context;

	private SharePlatformCallback onSharePlatformCallback;

	public void setOnSharePlatformCallback(
			SharePlatformCallback onSharePlatformCallback) {
		this.onSharePlatformCallback = onSharePlatformCallback;
	}

	public interface SharePlatformCallback {
		void success();
	}

	public SharePlatformActionListener() {
		
	}
	
	public SharePlatformActionListener(Activity context,SharePlatformCallback onSharePlatformCallback) {
		this.context = context;
		this.onSharePlatformCallback = onSharePlatformCallback;
	}
	
	public void init(Activity context,
			SharePlatformCallback onSharePlatformCallback) {
		this.context = context;
		this.onSharePlatformCallback = onSharePlatformCallback;
	}

	public void onComplete(Platform plat, int action,
			HashMap<String, Object> res) {
		Message msg = new Message();
		msg.arg1 = 1;
		msg.arg2 = action;
		msg.obj = plat;
		UIHandler.sendMessage(msg, this);
	}

	public void onCancel(Platform plat, int action) {
		Message msg = new Message();
		msg.arg1 = 3;
		msg.arg2 = action;
		msg.obj = plat;
		UIHandler.sendMessage(msg, this);
	}

	public void onError(Platform plat, int action, Throwable t) {
		t.printStackTrace();
		Message msg = new Message();
		msg.arg1 = 2;
		msg.arg2 = action;
		msg.obj = t;
		UIHandler.sendMessage(msg, this);
	}

	@Override
	public boolean handleMessage(Message msg) {
		String text = actionToString(msg.arg2);
		Logger.i(TAG, "分享成功后的回调执行了1111111111111111");
		switch (msg.arg1) {
		case 1:
			// 成功
			Logger.i(TAG, "分享成功后的回调执行222222222222");
			Platform plat = (Platform) msg.obj;
			if (SinaWeibo.NAME.equals(plat.getName())) {
				text = "新浪微博" + " "
						+ context.getString(R.string.share_completed);
			} else if (Wechat.NAME.equals(plat.getName())) {
				text = "微信" + " " + context.getString(R.string.share_completed);
			} else if (QQ.NAME.equals(plat.getName())) {
				text = "QQ好友" + " "
						+ context.getString(R.string.share_completed);
			} else if (QZone.NAME.equals(plat.getName())) {
				text = "QQ空间" + " "
						+ context.getString(R.string.share_completed);
			} else if (WechatMoments.NAME.equals(plat.getName())) {
				text = "朋友圈" + " "
						+ context.getString(R.string.share_completed);
			} else {
				text = context.getString(R.string.share_completed);
			}
			if (this.onSharePlatformCallback != null) {
				this.onSharePlatformCallback.success();
			}
			MobclickAgent.onEvent(context, "photographerVideoShare_id");
			ToastAlone.showToast(context, text, Toast.LENGTH_LONG);
			break;
		case 2:
			// 失败
			Logger.i("shareinfo", "error=" + text);
			if ("WechatClientNotExistException".equals(msg.obj.getClass()
					.getSimpleName())) {
				text = context.getString(R.string.wechat_client_inavailable);
			} else if ("WechatTimelineNotSupportedException".equals(msg.obj
					.getClass().getSimpleName())) {
				text = context.getString(R.string.wechat_client_inavailable);
			} else {
				Throwable t = (Throwable) msg.obj;
				Logger.i("shareinfo", "errorMessage=" + t.getMessage());
				text = context.getString(R.string.share_failed);
			}
			ToastAlone.showToast(context, text, Toast.LENGTH_LONG);
			break;
		case 3:
			// 取消
			Platform pf = (Platform) msg.obj;
			text = context.getString(R.string.share_canceled);
			if (pf != null) {
				if (SinaWeibo.NAME.equals(pf.getName())) {
					text = "新浪微博" + " "
							+ context.getString(R.string.share_canceled);
				} else if (QQ.NAME.equals(pf.getName())) {
					text = "QQ好友" + " "
							+ context.getString(R.string.share_canceled);
				} else if (QZone.NAME.equals(pf.getName())) {
					text = "QQ空间" + " "
							+ context.getString(R.string.share_canceled);
				} else if (Wechat.NAME.equals(pf.getName())) {
					text = "微信" + " "
							+ context.getString(R.string.share_canceled);
				} else if (WechatMoments.NAME.equals(pf.getName())) {
					text = "朋友圈" + " "
							+ context.getString(R.string.share_canceled);
				}
			}
			ToastAlone.showToast(context, text, Toast.LENGTH_LONG);
			break;
		}
		return false;
	}

	/** 将action转换为String */
	private String actionToString(int action) {
		switch (action) {
		case Platform.ACTION_AUTHORIZING:
			return "ACTION_AUTHORIZING";
		case Platform.ACTION_GETTING_FRIEND_LIST:
			return "ACTION_GETTING_FRIEND_LIST";
		case Platform.ACTION_FOLLOWING_USER:
			return "ACTION_FOLLOWING_USER";
		case Platform.ACTION_SENDING_DIRECT_MESSAGE:
			return "ACTION_SENDING_DIRECT_MESSAGE";
		case Platform.ACTION_TIMELINE:
			return "ACTION_TIMELINE";
		case Platform.ACTION_USER_INFOR:
			return "ACTION_USER_INFOR";
		case Platform.ACTION_SHARE:
			return "ACTION_SHARE";
		default:
			return "UNKNOWN";
		}
	}

	public void processOfShare(Activity context, int type) {
		if (type == 0) {// 成功
			if (this.onSharePlatformCallback != null) {
				this.onSharePlatformCallback.success();
			}
			ToastAlone.showToast(context, "分享成功", Toast.LENGTH_LONG);
		} else if (type == 1) {// 取消
			ToastAlone.showToast(context, "分享取消", Toast.LENGTH_LONG);
		} else if (type == 2) {// 失败
			ToastAlone.showToast(context, "分享失败", Toast.LENGTH_LONG);
		}
		Constant.setShare(false);
	}

	public void processOfLogin(Activity context, int type) {
		if (type == 1) {// 取消
			ToastAlone.showToast(context, "授权已取消", Toast.LENGTH_LONG);
		} else if (type == 2) {// 失败
			ToastAlone.showToast(context, "授权失败", Toast.LENGTH_LONG);
		}
		Constant.setLogin(false);
	}
}
